package com.romotc.qqrobot.bean;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.romotc.joyin.bean.Customer;
import com.romotc.joyin.bean.CustomerGrant;
import com.romotc.joyin.bean.CustomerGrantsService;
import com.romotc.joyin.bean.CustomerService;
import com.romotc.joyin.exception.CustomerException;
import com.romotc.joyin.exception.QQUserFriendException;
import com.romotc.oschina.db.QueryHelper;

public class QQUserFriendService {

	private static QQUserFriend quf = new QQUserFriend();
	/**
	 * @param string
	 * @return
	 */
	public static QQUserFriend qryQFriendByMarkName(String markName) {
		if(StringUtils.isNotBlank(markName))
		{
			List<QQUserFriend> result = (List<QQUserFriend>)quf.Filter("markname = '" + markName + "'", 1, 1);
			if(result != null && !result.isEmpty()) 
				return result.get(0);
		}
		return null;
	}

	
	public static void main(String args[]) {
//		QQUserFriend quf = qryQFriendByMarkName("d00098");
//		System.out.println(quf.getUin());
		
//		System.out.println(qryQFriendSizeByMarkName(670, "d00098"));
		QQUserFriend quFriend = new QQUserFriend();
		quFriend.setId(1);
		updateQFriendCustId(quFriend, 222);
	}


	/**
	 * 根据loginId, uin查询userFriends
	 * @param loginId
	 * @param qqUin
	 * @return
	 */
	public static QQUserFriend qryQFriendByUin(long loginId, long qqUin) {
		List<QQUserFriend> result = (List<QQUserFriend>)quf
			.Filter("loginId = " + loginId + " and uin = " + qqUin, 1, 1);
		if(result != null && !result.isEmpty()) 
			return result.get(0);
		
		return null;
	}


	/**
	 * 查询markName的数量
	 * @param loginId
	 * @param markname
	 * @return
	 */
	public static int qryQFriendSizeByMarkName(long loginId, String markname) {
		return quf.TotalCount("loginId = " + loginId + " and markname = '" + markname + "'");
	}


	/**
	 * 查询授权的客户 并转换成好友
	 * @param curQQ
	 * @return
	 */
	public static List<QQUserFriend> qryGrantFriends(QQLogedin curQQ) {
		long csId = curQQ.getCsId();//客服Id
		if(csId < 1) {
			return null;//防止非客服工号调进来
		}
		
		//查询出来授权的客户
		List<CustomerGrant> grants = CustomerGrantsService.qryCustServiceGrantsList(csId);
		
		
		//根据客户组织friends
		return buildFriendsByCustomers(grants);
	}


	/**
	 * 根据grants.getCustomer组织好友
	 * @param grants
	 * @return
	 */
	public static List<QQUserFriend> buildFriendsByCustomers(
									List<CustomerGrant> grants) {
		if(grants == null || grants.isEmpty())
			return null;
		
		List<QQUserFriend> userFriends = new ArrayList<QQUserFriend>(grants.size());
		
		Customer cust;
		QQUserFriend quf;
		for(CustomerGrant grant : grants) {
			if(grant == null)
				continue;
			
			cust = grant.customer();
			
			if(cust == null)
				continue;
			
			quf = QQUserFriend.createQUF(cust);
			userFriends.add(quf);
		}
		return userFriends;
	}


	/**
	 * 根据客户Id查找quf
	 * @param senderQQ
	 * @param custId
	 * @return
	 */
	public static QQUserFriend qryQFriendByCustId(QQLogedin senderQQ, long custId) throws Exception{
		
		//首先查询qq_user_friend表 看之前有没有设置custId
		QQUserFriend quf = qryQFriendByCustIdInTableQQUserFriend(senderQQ, custId);
		
		if(quf != null)
			return quf;
		
		//如果之前没有设置则根据custId查询客户表 (然后根据客户姓名查询qq_user_friend表)
		Customer cust = CustomerService.qryCustomerById(custId);
		if(cust == null)
			throw new CustomerException("未查询到指定客户");
		
		//根据客户姓名查询qq_user_friend表
		quf = qryQFriendByMarkName(senderQQ.getId(), cust.getCustName());
		if(quf != null)		
			//查询出来之后给qq_user_friend设置上custId
			updateQFriendCustId(quf, cust.getId());
		
		return quf;
	}

	/**
	 *设置数据表对应记录的custId
	 * @param quf2 记录对应的Obj
	 * @param custId 客户Id
	 */
	public static void updateQFriendCustId(QQUserFriend quf2, long custId) {
		if(quf2 == null)
			return ;
		String sql = "UPDATE " + quf2.TableName() + " SET custId = ? where id = ?";
		QueryHelper.update(sql, custId, quf2.getId());
	}


	/**
	 * 根据客户custName(qquserFriend markName)查询qqUserFriend
	 * @param loginId
	 * @param custName
	 * @return
	 */
	public static QQUserFriend qryQFriendByMarkName(long loginId, String custName) {
		String filter = "loginId = " + loginId + " and markname = '" + custName + "'";
		List<QQUserFriend> qufList = (List<QQUserFriend>)quf.Filter(filter, 1, 1);
		if(qufList == null || qufList.isEmpty())
			return null;
		
		return qufList.get(0);
	}


	/**
	 * 查询：了l:根据custid查询qquserfriend
	 * @param senderQQ
	 * @param custId
	 * @return
	 */
	public static QQUserFriend qryQFriendByCustIdInTableQQUserFriend(
			QQLogedin senderQQ, long custId) {
		if(senderQQ == null)
			return null;
		
		String filter = "loginId = " + senderQQ.getId() + " and custId = " + custId;
		List<QQUserFriend> list = (List<QQUserFriend>)quf.Filter(filter, 1, 1);
		if(list == null || list.isEmpty())
			return null;
		return list.get(0);
	}


}
